package com.angcyo.library

import kotlin.random.Random.Default.nextInt

/**
 * Email:angcyo@126.com
 * @author angcyo
 * @date 2021/01/20
 */
object Code {

    /**
     * 创建一个验证码
     * https://github.com/lingd3/Captcha
     * 返回 code 和 图片
     * */
    fun generate(length: Int = 4): String {
        //字典池, 去掉难分辨的字符O0
        val ch = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789".toCharArray()
        val len = ch.size

        var index: Int
        val sb = StringBuffer()
        for (i in 0 until length) {
            index = nextInt(len)
            //设置x y 坐标
            val c = ch[index]
            sb.append(c)
        }

        return sb.toString()
    }

}